package com.cjt2325.cameralibrary.util;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * ===================================== 作 者: 陈嘉桐 版 本：1.1.4 创建日期：2017/4/25 描 述：
 * =====================================
 */
public class FileUtil {
	private static final String TAG = "CJT";
	private static final File parentPath = Environment.getExternalStorageDirectory();
	private static String storagePath = "";
	private static String DST_FOLDER_NAME = "JCamera";

	private static String initPath() {
		if (storagePath.equals("")) {
			storagePath = parentPath.getAbsolutePath() + File.separator + DST_FOLDER_NAME;
			File f = new File(storagePath);
			if (!f.exists()) {
				f.mkdir();
			}
		}
		return storagePath;
	}

	public static String saveBitmap(String dir, Bitmap b) {
		DST_FOLDER_NAME = dir;
		String path = initPath();
		long dataTake = System.currentTimeMillis();
		String jpegName = path + File.separator + "picture_" + dataTake + ".jpg";
		try {
			FileOutputStream fout = new FileOutputStream(jpegName);
			BufferedOutputStream bos = new BufferedOutputStream(fout);
			b.compress(Bitmap.CompressFormat.JPEG, 100, bos);
			bos.flush();
			bos.close();
			return jpegName;
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static boolean isExternalStorageWritable() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		}
		return false;
	}

	public static String formatTime(long milliseconds) {
		return new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.getDefault()).format(new Date(milliseconds));
	}
}
